package com.cody.ssmmvp.controller;

import com.alibaba.fastjson.JSON;
import com.cody.ssmmvp.pojo.User;
import com.cody.ssmmvp.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by cody on 16/2/24.
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    private static final String VIEW_SHOW_USER = "showUser";

    @RequestMapping("/showUser")
    public String toIndex(HttpServletRequest request, Model model){
        int userId = 0;
        try {
            userId = Integer.parseInt(request.getParameter("id"));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        if (userId > 0) {
            User user = this.userService.getUserById(userId);
            model.addAttribute("user",user);
            model.addAttribute("userJsonString", JSON.toJSONString(user));
            model.addAttribute("userJsonStringPrettyFalse",JSON.toJSONString(user,false));
            model.addAttribute("userJsonStringPrettyTrue",JSON.toJSONString(user,true));
        }
        return VIEW_SHOW_USER;
    }

}
